package com.ls.pattern.builder;


import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

@Getter
public class PersonModel {

    private String name;
    private Integer age;
    private String phone;
    private String email;

    public PersonModel(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.phone = builder.phone;
        this.email = builder.email;
    }

    public static class Builder {
        private String name;
        private Integer age;
        private String phone;
        private String email;

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(Integer age) {
            this.age = age;
            return this;
        }

        public Builder phone(String phone) {
            this.phone = phone;
            return this;
        }

        public Builder email(String email) {
            this.email = email;
            return this;
        }

        public PersonModel build() {
            if (StringUtils.isBlank(name)) {
                throw new RuntimeException("姓名不能为空");
            }
            if (age == null) {
                throw new RuntimeException("年龄不能为空");
            }
            if (StringUtils.isBlank(phone) && StringUtils.isBlank(email)) {
                throw new RuntimeException("联系方式和邮箱不能同时为空");
            }
            return new PersonModel(this);
        }
    }

}
